package com.qianyitian.hope2.spider.gaokao;

import com.alibaba.fastjson.JSON;
import com.google.common.io.Files;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.List;

public class JSoupDataFetcher {


    public List<OneFenOneDuan> get1Fen1DuanTablePerPage(String link) throws IOException {
        List<OneFenOneDuan> list = new ArrayList<>();

        Document doc = Jsoup.connect(link).get();
        Element table = doc.select("table.bgddd").first();
        Elements trList = table.select("tr.tb_content");
        for (Element tr : trList) {
            Elements tdList = tr.select("td");
            OneFenOneDuan row = new OneFenOneDuan();
            row.setZone(tdList.get(0).text());
            row.setYear(tdList.get(1).text());
            row.setType(tdList.get(2).text());
            row.setScore(tdList.get(3).text());
            row.setNumber(Integer.valueOf(tdList.get(4).text()));
            row.setPosition(Integer.valueOf(tdList.get(5).text()));
            list.add(row);
        }
        return list;
    }

    String URL_WENKE = "http://www.haochafen.com/yfyd-2681982-2021-2682005-0-{0}.html";
    String URL_LIKE = "http://www.haochafen.com/yfyd-2681982-2021-2682006-0-{0}.html";

    public void get1Fen1DuanTable() throws IOException {
        {
            List<OneFenOneDuan> all = new ArrayList<>();
            for (int i = 1; i <= 11; i++) {
                String link = MessageFormat.format(URL_WENKE, i);
                List<OneFenOneDuan> fen1DuanTablePerPage = get1Fen1DuanTablePerPage(link);
                all.addAll(fen1DuanTablePerPage);
            }
            File file = new File("2021-wenke.json");
            Files.asCharSink(file, Charset.forName("UTF-8")).write(JSON.toJSONString(all,true));
        }

        {
            List<OneFenOneDuan> all = new ArrayList<>();
            for (int i = 1; i <= 11; i++) {
                String link = MessageFormat.format(URL_LIKE, i);
                List<OneFenOneDuan> fen1DuanTablePerPage = get1Fen1DuanTablePerPage(link);
                all.addAll(fen1DuanTablePerPage);
            }
            File file = new File("2021-like.json");
            Files.asCharSink(file, Charset.forName("UTF-8")).write(JSON.toJSONString(all,true));
        }





    }
}
